package com.ruoyi.web.controller.campus;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.campus.domain.MyApply;
import com.ruoyi.campus.service.IMyApplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 悬赏榜领取申请Controller
 *
 * @author zht
 * @date 2022-02-12
 */
@RestController
@RequestMapping("/campus/apply")
public class MyApplyController extends BaseController {
    @Autowired
    private IMyApplyService myApplyService;

    /**
     * 申请领取悬赏榜(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @PostMapping("/applyOffer")
    public AjaxResult applyOffer(@RequestBody MyApply myApply) {
        myApply.setApplyId(UUID.randomUUID().toString().replace("-", ""));
        return toAjax(myApplyService.insertMyApply(myApply));
    }

    /**
     * 获取个人申请的悬赏榜Id(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/getMyApplyOfferId")
    public AjaxResult getMyApplyOfferId(@RequestParam("userId") String userId) {
        List<String> list = myApplyService.getMyApplyOfferId(userId);
        if (list != null && list.size() > 0){
            return AjaxResult.success(list);
        }else {
            return AjaxResult.error();
        }
    }

    /**
     * 获取个人要审核的悬赏榜信息(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/getMyAuditOffer")
    public TableDataInfo getMyAuditOffer(@RequestParam("iuserId") String iuserId){
        startPage();
        List<Map<String,Object>> list = myApplyService.getMyAuditOffer(iuserId);
        return getDataTable(list);
    }

    /**
     * 拒绝通过申请(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/refuseApply")
    public AjaxResult refuseApply(@RequestParam("applyId") String applyId){
        return toAjax(myApplyService.deleteMyApplyByApplyId(applyId));
    }

    /**
     * 同意通过申请(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/agreeApply")
    public AjaxResult agreeApply(@RequestParam("offerId") String offerId, @Param("ruserId") String ruserId){
        return toAjax(myApplyService.agreeApply(offerId,ruserId));
    }

    /**
     * 获取我申请的悬赏榜信息(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/getMyApplyOffer")
    public TableDataInfo getMyApplyOffer(@RequestParam("userId") String userId){
        startPage();
        List<Map<String,Object>> list = myApplyService.getMyApplyOffer(userId);
        return getDataTable(list);
    }

    /**
     * 查询悬赏榜领取申请列表
     */
    @PreAuthorize("@ss.hasPermi('campus:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyApply myApply) {
        startPage();
        List<MyApply> list = myApplyService.selectMyApplyList(myApply);
        return getDataTable(list);
    }

    /**
     * 导出悬赏榜领取申请列表
     */
    @PreAuthorize("@ss.hasPermi('campus:apply:export')")
    @Log(title = "悬赏榜领取申请", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyApply myApply) {
        List<MyApply> list = myApplyService.selectMyApplyList(myApply);
        ExcelUtil<MyApply> util = new ExcelUtil<MyApply>(MyApply.class);
        return util.exportExcel(list, "悬赏榜领取申请数据");
    }

    /**
     * 获取悬赏榜领取申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:apply:query')")
    @GetMapping(value = "/{applyId}")
    public AjaxResult getInfo(@PathVariable("applyId") String applyId) {
        return AjaxResult.success(myApplyService.selectMyApplyByApplyId(applyId));
    }

    /**
     * 新增悬赏榜领取申请
     */
    @PreAuthorize("@ss.hasPermi('campus:apply:add')")
    @Log(title = "悬赏榜领取申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyApply myApply) {
        return toAjax(myApplyService.insertMyApply(myApply));
    }

    /**
     * 修改悬赏榜领取申请
     */
    @PreAuthorize("@ss.hasPermi('campus:apply:edit')")
    @Log(title = "悬赏榜领取申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyApply myApply) {
        return toAjax(myApplyService.updateMyApply(myApply));
    }

    /**
     * 删除悬赏榜领取申请
     */
    @PreAuthorize("@ss.hasPermi('campus:apply:remove')")
    @Log(title = "悬赏榜领取申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{applyIds}")
    public AjaxResult remove(@PathVariable String[] applyIds) {
        return toAjax(myApplyService.deleteMyApplyByApplyIds(applyIds));
    }
}
